﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using Controller.DataProvider;
using System.Data;
//////////////////////////////////
using BaseUtilities;
using ManageStore.IViews;
using System.Collections.ObjectModel;

namespace Controller
{
    public class ProductTypeCtr : BaseCtr, IDataErrorInfo
    {
        public ProductTypeCtr(IProductTypeView pIProductTypeView)
        {
            this.IProductTypeView = pIProductTypeView;
        }

        #region Properties
        // IProductTypeView.
        public IProductTypeView IProductTypeView { get; set; }
        // ProductTypePro.
        private ProductTypePro _ProductTypePro;
        public ProductTypePro ProductTypePro
        {
            get
            {
                if (_ProductTypePro == null)
                {
                    _ProductTypePro = new ProductTypePro();
                }
                return _ProductTypePro;
            }
            set { _ProductTypePro = value; }
        }
        // ProductType.
        private ProductType _ProductType;
        public ProductType ProductType
        {
            get
            {
                if (_ProductType == null)
                {
                    _ProductType = new ProductType();
                }
                return _ProductType;
            }
            set { _ProductType = value; OnPropertyChanged("ProductType"); }
        }
        // LstErrorFields
        private Dictionary<string, string> _DicError;
        public Dictionary<string, string> DicError
        {
            get
            {
                if (_DicError == null)
                    _DicError = new Dictionary<string, string>();
                return _DicError;
            }
            set
            {
                _DicError = value;
            }
        }
        #endregion Properties

        #region Binding properties
        // Id
        public int Id
        {
            get
            {
                return ProductType.Id;
            }
            set
            {
                if (ProductType.Id != value)
                {
                    ProductType.Id = value;
                    //OnPropertyChanged("Id");
                }
            }
        }
        // ParentId
        public int? ParentId
        {
            get
            {
                return ProductType.ParentId;
            }
            set
            {
                if (ProductType.ParentId != value)
                {
                    if (value.HasValue && value.Value == 0)
                        ProductType.ParentId = null;
                    else
                        ProductType.ParentId = value;
                    //OnPropertyChanged("ParentId");
                }
            }
        }
        // ProductTypeName
        public string ProductTypeName
        {
            get
            {
                return ProductType.Name;
            }
            set
            {
                if (ProductType.Name != value)
                {
                    ProductType.Name = value;
                    //OnPropertyChanged("ProductTypeName");
                }
            }
        }
        // Code
        public string ProductTypeCode
        {
            get
            {
                return ProductType.Code;
            }
            set
            {
                if (ProductType.Code != value)
                {
                    ProductType.Code = value;
                    //OnPropertyChanged("ProductTypeCode");
                }
            }
        }
        // ParentName
        public string ParentName
        {
            get
            {
                return ProductType.ParentName;
            }
            set
            {
                if (ProductType.ParentName != value)
                {
                    ProductType.ParentName = value;
                    //OnPropertyChanged("ParentName");
                }
            }
        }
        // Note
        public string Note
        {
            get
            {
                return ProductType.Note;
            }
            set
            {
                if (ProductType.Note != value)
                {
                    ProductType.Note = value;
                    //OnPropertyChanged("Note");
                }
            }
        }
        // LstProductType.
        private ObservableCollection<ProductType> _LstProductType;
        public ObservableCollection<ProductType> LstProductType
        {
            get
            {
                if (_LstProductType == null)
                {
                    _LstProductType = new ObservableCollection<BusinessModel.ProductType>(MtdGetAll().ToResult<IList<ProductType>>());
                }
                return _LstProductType;
            }
            set
            {
                if (_LstProductType != value)
                {
                    _LstProductType = value;
                    //OnPropertyChanged("LstProductType");
                }
            }
        }
        #endregion Binding properties

        #region Private Method
        //----------------------------------------
        private void LoadListProductType()
        {
            LstProductType = new ObservableCollection<BusinessModel.ProductType>(MtdGetAll().ToResult<IList<ProductType>>());
        }
        //----------------------------------------
        private ResultModel MtdGetById(int pProductTypeId)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductTypePro.EntityName, "Id", pProductTypeId);
            return ProductTypePro.GetById(oEntityKey);
        }
        //----------------------------------------
        private ResultModel MtdGetAll()
        {
            return ProductTypePro.GetAll();
        }
        #endregion Private Method

        #region Public View Handlers
        //----------------------------------------
        public bool MtdNew(params object[] pParams)
        {
            bool result = false;
            if (IProductTypeView.BeforeHandler("EvtNew"))
            {
                LoadListProductType();
                IProductTypeView.ReloadData();
                ProductType = new ProductType();
                DicError.Clear();
                IProductTypeView.CompletedHandler("EvtNew");
            }
            return result;
        }
        //----------------------------------------
        public bool MtdEdit(params object[] pParams)
        {
            bool result = false;
            if (IProductTypeView.BeforeHandler("EvtEdit"))
            {
                if (pParams.Count() == 1)
                {
                    ProductType = pParams[0] as ProductType;
                    if (ProductType == null)
                    {
                        IProductTypeView.ErrorHandler("EvtEdit");
                    }
                    else
                    {
                        DicError.Clear();
                        IProductTypeView.CompletedHandler("EvtEdit");
                    }
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdDelete(params object[] pParams)
        {
            bool result = false;
            if (IProductTypeView.BeforeHandler("EvtDelete"))
            {
                EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductTypePro.EntityName, "Id", ProductType.Id);
                ResultModel oResultModel = ProductTypePro.DeleteById(oEntityKey);
                if (oResultModel.HasError)
                {
                    IProductTypeView.ErrorHandler("EvtDelete", oResultModel);
                }
                else
                {
                    LstProductType = new ObservableCollection<BusinessModel.ProductType>(MtdGetAll().ToResult<IList<ProductType>>());
                    IProductTypeView.CompletedHandler("EvtDelete");
                    result = true;
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdSave(params object[] pParams)
        {
            // Check validate.
            if (DicError.Count > 0)
            {
                IProductTypeView.ErrorHandler("EvtSave", DicError);
                return false;
            }
            bool result = false;
            if (IProductTypeView.BeforeHandler("EvtSave"))
            {
                if (ProductType.Id == default(int))
                {
                    ProductType.CreatedBy = ControllerConfig.Default.UserName;
                    ProductType.CreatedDate = DateTime.Now;
                }
                ProductType.ModifiedBy = ControllerConfig.Default.UserName;
                ProductType.ModifiedDate = DateTime.Now;
                
                EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductTypePro.EntityName, "Id", ProductType.Id);
                ResultModel oResultModel = ProductTypePro.Save(ProductType, oEntityKey);
                if (oResultModel.HasError)
                {
                    IProductTypeView.ErrorHandler("EvtSave", oResultModel);
                }
                else
                {
                    LstProductType = new ObservableCollection<BusinessModel.ProductType>(MtdGetAll().ToResult<IList<ProductType>>());
                    IProductTypeView.CompletedHandler("EvtSave", oResultModel.Result);
                    result = true;
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdGetAllChilds(Action<object> pAction, params object[] pParams)
        {
            bool result = false;
            int? iParentId = null;
            if(pParams.Count() == 1)
            {
                iParentId = int.Parse(pParams[0].ToString());
            }
            IList<ProductType> lstPro = ProductTypePro.GetAllChilds(iParentId).ToResult<IList<ProductType>>();
            pAction(lstPro);
            return result;
        }
        #endregion Public View Handlers

        #region IDataErrorInfo Members
        private string _Error;
        public string Error
        {
            get
            {
                return _Error;
            }
        }
        public string this[string columnName]
        {
            get
            {
                string error = string.Empty;
                if (DicError.Keys.Contains(columnName))
                    DicError.Remove(columnName);
                switch (columnName)
                {
                    case "ProductTypeCode":
                        if (string.IsNullOrWhiteSpace(ProductTypeCode))
                        {
                            error = "Mã loại không được rỗng.";
                        }
                        else
                        {
                            if(Boolean.Parse(ProductTypePro.CheckProductTypeCode(Id, ProductTypeCode).Result.ToString()))
                                error = "Mã loại đã tồn tại.";
                        }
                        break;
                    case "ProductTypeName":
                        if (string.IsNullOrWhiteSpace(ProductTypeName))
                        {
                            error = "Tên loại không được rỗng.";
                        }
                        break;
                }
                _Error = error;
                if (!string.IsNullOrWhiteSpace(error))
                {
                    DicError.Add(columnName, error);
                }
                IProductTypeView.ValidationHandler(columnName, error);
                return error;
            }
        }
        #endregion
    }
}
